package com.JavaWk_3;

import java.util.Scanner;

class AgeException extends Exception{
    AgeException(String a){
        super(a);
    }
}

class IDException extends Exception{
    IDException(String a){
        super(a);
    }
}

class PhoneException extends Exception{
    PhoneException(String a){
        super(a);
    }
}


class Student{
    private int age;
    private String name, ID, p_Number;

    public Student(){}

    public Student(String n,int a,String i,String num){
        super();
        this.name = n;
        this.age = a;
        this.ID = i;
        this.p_Number = num;
    }

    public String toString(){
        return "姓名: " + this.name + " 年龄: " + this.age + " 身份证号: " + this.ID + " 电话: " + this.p_Number + ".";
    }

    public void setAge(int age) throws AgeException{
        if(age>=18 && age<=50){
            this.age = age;
        }else{
            throw new AgeException("年龄错误，年龄范围为[18,50]");
        }
    }
    public void setID(String id) throws IDException{
        String matchString = "(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$)";
        if(id.matches(matchString)){
            this.ID = id;
        }else{
            throw new IDException("身份证号格式错误");
        }
    }

    public void setP_Number(String num) throws PhoneException{
        String matchString = "^[1][3,4,5,7,8][0-9]{9}$";
        if(num.matches(matchString)){
            this.p_Number = num;
        }else{
            throw new PhoneException("手机号格式出错");
        }
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public int getAge(){
        return this.age;
    }

    public String getID(){
        return this.ID;
    }

    public String getP_Number(){
        return this.p_Number;
    }

}

public class work3 {
    static final int N = 3;

    public static Student find_Name(Student[] st, String name){
        for(Student e:st){
            if(e.getName().equals(name)){
                return e;
            }
        }
        return null;
    }
    public static Student find_Id(Student[] st, String id){
        for(Student e:st){
            if(e.getID().equals(id)){
                return e;
            }
        }
        return null;
    }
    public static Student find_PhoneNumber(Student[] st, String pid){
        for(Student e:st){
            if(e.getP_Number().equals(pid)){
                return e;
            }
        }
        return null;
    }
    public static Student[] sort_Stu(Student[] s){
        Student t = s[0];
        for(int i = 0; i < s.length - 1; i ++ ){
            t = s[i];
            for(int j = i + 1; j < s.length; j ++ ){
                if(t.getAge() > s[j].getAge()){
                    s[i] = s[j];
                    s[j] = t;
                }
            }
        }
        return s;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Student[] stu = new Student[N];
        for(int i = 0;i < N; i ++ ){
            stu[i] = new Student();

            System.out.print("输入学生姓名,年龄,身份证号,电话号码：");
            stu[i].setName(sc.next());

            try {
                stu[i].setAge(sc.nextInt());
            } catch (AgeException e) {
                System.err.println(e.getMessage());
            }

            try {
                stu[i].setID(sc.next());
            } catch (IDException e) {
                System.err.println(e.getMessage());
            }

            try {
                stu[i].setP_Number(sc.next());
            } catch (PhoneException e) {
                System.err.println(e.getMessage());
            }

        }
        System.out.print("输入要查找的学生姓名：");
        System.out.println("该学生的信息为："+ find_Name(stu,sc.next()));

        System.out.print("输入要查找的学生身份证号：");
        System.out.println("该学生的信息为："+ find_Id(stu,sc.next()));

        System.out.print("输入要查找的学生电话号码：");
        System.out.println("该学生的信息为："+ find_PhoneNumber(stu,sc.next()));

        System.out.println("排序前：");
        for (Student student : stu) {
            System.out.println(student);
        }

        stu = sort_Stu(stu);

        System.out.println("排序后：");
        for (Student student : stu) {
            System.out.println(student);
        }
    }

}

